package com.zrrd.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.reggie.bean.OrderDetail;
import com.zrrd.reggie.bean.Orders;
import com.zrrd.reggie.bean.dto.OrderDto;
import com.zrrd.reggie.common.BaseContext;
import com.zrrd.reggie.common.CustException;
import com.zrrd.reggie.common.R;
import com.zrrd.reggie.service.OrderDetailService;
import com.zrrd.reggie.service.OrdersService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrderDetailService orderDetailService;

    @GetMapping("page")
    public R<Page> page(Integer page, Integer pageSize,String number,
                        String beginTime,String endTime){
        Page<Orders> pageInfo=new Page<>(page,pageSize);
        QueryWrapper<Orders> wrapper=new QueryWrapper<>();
        if (number !=null&&!number.equals("")){
            wrapper.eq("number",number);
        }
        if (beginTime!=null && !beginTime.equals("") && endTime!=null && !endTime.equals("")){
            wrapper.between("order_time",beginTime,endTime);
        }
        ordersService.page(pageInfo,wrapper);
        return R.sucess(pageInfo);

    }

    @GetMapping("userPage")
    private R<Page> userPage(Integer page,Integer pageSize){
        Page<Orders> pageInfo=new Page<>(page,pageSize);
        QueryWrapper<Orders> wrapper=new QueryWrapper<>();
        wrapper.eq("user_id", BaseContext.getCurrentId());
        wrapper.orderByDesc("order_time");
        ordersService.page(pageInfo,wrapper);
        Page<OrderDto> dtoPage=new Page<>();
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");
        List<OrderDto> dtoList=new ArrayList<>();
        List<Orders> orders=pageInfo.getRecords();
        for (Orders order : orders) {
            OrderDto dto=new OrderDto();
            BeanUtils.copyProperties(order,dto);
            QueryWrapper<OrderDetail> detailQueryWrapper=new QueryWrapper<>();
            detailQueryWrapper.eq("order_id",order.getId());
            List<OrderDetail> detailList = orderDetailService.list(detailQueryWrapper);
            dto.setOrderDetails(detailList);
            dtoList.add(dto);
        }
        dtoPage.setRecords(dtoList);
        return R.sucess(dtoPage);
    }

    @PostMapping("again")
    public R again(@RequestBody Orders orders){
        ordersService.again(orders);
        return R.sucess("再来一单成功");
    }
    @PutMapping
    public R<String> updateStatus(@RequestBody Orders orders){
        return ordersService.updateById(orders)?R.sucess("修改成功"):R.error("修改失败");
    }
    @PostMapping("submit")
    public R<String> submit(@RequestBody Orders orders) throws CustException {
        ordersService.saveOrder(orders);

        return R.sucess("添加成功");
    }

}
